/*
https://leetcode-cn.com/problems/split-array-into-consecutive-subsequences/solution/fen-ge-shu-zu-wei-lian-xu-zi-xu-lie-by-l-lbs5/
 */
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Solution659 {
    public boolean isPossible(int[] nums) {
        Map<Integer, PriorityQueue<Integer>> map=new HashMap<>();
        for (int num : nums) {
            PriorityQueue<Integer> queue=map.getOrDefault(num-1,new PriorityQueue<>());
            PriorityQueue<Integer> q2=map.getOrDefault(num,new PriorityQueue<>());
            if (!queue.isEmpty()){
                int t=queue.poll();
                q2.offer(t+1);
                map.put(num-1,queue);
            }else{
                q2.offer(1);
            }
            map.put(num,q2);
        }
        for (Map.Entry<Integer, PriorityQueue<Integer>> integerPriorityQueueEntry : map.entrySet()) {
            if (!integerPriorityQueueEntry.getValue().isEmpty() && integerPriorityQueueEntry.getValue().peek() <3){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution659().isPossible(new int[]{1,2,3,4,4,5}));
    }
}
